import Resources.Enums as Enums
from Classes import Token


def ReadKeyWords():
    fs = open('./LanguageToCode/Resources/Keywords', 'r', encoding='utf-8')
    keyWordsDict = dict()
    for line in fs.readlines():
        if line[-1] == '\n':
            line = line[0:-1]
        temList = line.split(' ')
        if len(temList) < 2:
            continue
        keyWordsDict[temList[0]] = Enums.Tag[temList[1]]
    fs.close()
    return keyWordsDict


def ReadTags():
    fs = open('./LanguageToCode/Resources/Tags', 'r', encoding='utf-8')
    tagDict = dict()
    for line in fs.readlines():
        if line[-1] == '\n':
            line = line[0:-1]
        temList = line.split(' ')
        if len(temList) < 2:
            continue
        tagDict[Enums.Tag[temList[0]]] = Enums.Kind[temList[1]]
    fs.close()
    return tagDict


def Tokenize(strList):
    ret = []
    keyDict = ReadKeyWords()
    tagDict = ReadTags()
    for s in strList:
        sentence = []
        temLength = len(s)
        cur = 0
        while cur < temLength:
            for i in range(temLength):
                if temLength - i <= cur:
                    break
                tempStr = s[cur:temLength - i]
                if tempStr in keyDict:
                    temTag = keyDict[tempStr]
                    temKind = tagDict[temTag]
                    sentence.append(Token(temTag, temKind))
                    cur = temLength - i - 1
                    break
            cur = cur + 1
        ret.append(sentence)
    return ret
